package com.zhangtai.modules.controller.admin;


import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.zhangtai.modules.controller.aouthLogin.LoginController;
import com.zhangtai.modules.controller.aouthLogin.MemberLoginController;
import com.zhangtai.modules.dto.EmployeeOverworkDto;
import com.zhangtai.modules.dto.EmployeeOverworkPageDto;
import com.zhangtai.modules.dto.OverworkRecordDto;
import com.zhangtai.modules.entity.employee.EmployeeInfoEntity;
import com.zhangtai.modules.entity.employee.EmployeeOverworkEntity;
import com.zhangtai.modules.service.employee.EmployeeInfoService;
import com.zhangtai.modules.service.employee.EmployeeOverworkService;
import com.zhangtai.utils.PageResult;
import com.zhangtai.utils.PageUtils;
import com.zhangtai.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;


/**
 *
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2020-12-07 15:29:22
 */
@Api(tags="自助员工加班")
@RestController
@RequestMapping("employeeOverwork")
@Slf4j
public class EmployeeOverworkController {
    @Autowired
    private EmployeeOverworkService employeeOverworkService;

    @Autowired
    private EmployeeInfoService employeeInfoService;
    @Autowired
    private MemberLoginController memberLoginController;
    @Autowired
    private LoginController loginController;

    /**
     * 分页查询-加班申请
     * @param oprationName
     * @param pageNum
     * @param pageSize
     * @return
     */
    @ApiOperation("分页查询-加班申请")
    @GetMapping("/listEmployeeOverWork")
    public R<List<EmployeeOverworkPageDto>> list(@RequestParam(value = "oprationName", required = false) String oprationName,
                                                 @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                 @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize, HttpServletRequest request) throws Exception {

        JSONObject data = memberLoginController.getUserInfo(request).getData();
        if(data==null){
            return R.error("请先登录");

        }
        Long oid = data.getLong("id"); //只能查询自己的信息
        EmployeeInfoEntity employeeInfoEntity = employeeInfoService.getByIdE(oid);
        if (ObjectUtils.isEmpty(employeeInfoEntity) ){
            return R.error("请先登录");
        }
        PageResult pageResult = employeeOverworkService.queryPage(oprationName, pageNum, pageSize,request);

        return R.ok(pageResult);
    }

    /**
     * hr端分页查询-加班申请
     * @param oprationName
     * @param pageNum
     * @param pageSize
     * @return
     */
    @ApiOperation("hr端分页查询-加班申请")
    @GetMapping("/listEmployeeOverWorkHr")
    public R<List<EmployeeOverworkPageDto>> listHr(@RequestParam(value = "oprationName", required = false) String oprationName,
                                                   @RequestParam(value = "corpName", required = false) String corpName,
                                                   @RequestParam(value = "orgNname", required = false) String orgNname,
                                                   @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                 @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize, HttpServletRequest request) throws Exception {

        R uR = loginController.getUserInfo(request);
        //获取当前用户角色下公司Id
        R checkR = loginController.checkOrgPermission(request,null,false);
        if(!R.isOk.equals(checkR.getStatus())){
            return checkR;
        }
        List<Long> orgIds = (List<Long>) checkR.getData();
        PageResult pageResult = employeeOverworkService.queryPageHr(oprationName,corpName,orgNname, pageNum, pageSize,request);

        return R.ok(pageResult);
    }

    /**
     * 分页查询-加班记录
     * @param name
     * @param pageNum
     * @param pageSize
     * @return
     */
    @ApiOperation("分页查询-加班记录")
    @GetMapping("/listRecordOverWork")
    public R<List<OverworkRecordDto>> listRecord(@RequestParam(value = "name", required = false) String name,
                                                 @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                 @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize
            ,HttpServletRequest request) throws Exception {
        JSONObject data = memberLoginController.getUserInfo(request).getData();
        Long oid = data.getLong("id"); //只能查询自己的信息
        EmployeeInfoEntity employeeInfoEntity = employeeInfoService.getByIdE(oid);
        if (ObjectUtils.isEmpty(employeeInfoEntity)){
            return R.error("请先登录");
        }
        PageResult pageResult = employeeOverworkService.queryRecordPage(name, pageNum, pageSize,request);

        return R.ok(pageResult);
    }
    /**
     * 信息
     */
    @ApiOperation("根据id查询-加班申请")
    @GetMapping("/findById")
    public R<EmployeeOverworkDto> info(@RequestParam(value = "id",required = false) Long id){
        EmployeeOverworkDto byIdE = employeeOverworkService.getByIdE(id);

        return R.ok(byIdE);
    }

    /**
     * 保存-加班申请
     */
    @ApiOperation("保存-加班申请")
    @PostMapping("/saveOverWork")
    public R save(@RequestBody EmployeeOverworkDto employeeOverworkDto,HttpServletRequest request) throws Exception {
        employeeOverworkService.save(employeeOverworkDto, request);

        return R.ok();
    }

//    /**
//     * 修改
//     */
//    @RequestMapping("/update")
//    public R update(@RequestBody EmployeeOverworkEntity employeeOverwork){
//        employeeOverworkService.updateByIdE(employeeOverwork);
//
//        return R.ok();
//    }


    /**
     * 根据id删除-加班申请
     * @param id
     * @return
     */
    @ApiOperation("删除-根据id删除加班申请")
    @DeleteMapping("/deleteOverWorkById")
    public R delete(@RequestParam(value = "id",required = false) Long id){
        employeeOverworkService.deletByIdE(id);

        return R.ok();
    }




}

